/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import pojo.Register;

/**
 *
 * @author Mostafa
 */
public class RegisterDao {
  private   Connection c;

    public RegisterDao(Connection c) {
        this.c = c;
    }
    
    public void insert(Register r)
    {
        try {
            PreparedStatement ps=c.prepareStatement("insert into register values (?,?,?,?,?,?,?)");
            ps.setInt(1, r.getRId());
            ps.setString(2, r.getFullName());
            ps.setString(3, r.getUserName());
            ps.setString(4, r.getPassword());
            ps.setString(5, r.getType());
            ps.setString(6, r.getEmail());
            ps.setString(7, r.getPhone());
            ps.execute();
        } catch (SQLException ex) {
            Logger.getLogger(RegisterDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    public Register getById(Integer id) {
        try {
            Register r=new Register();
            
            PreparedStatement ps=c.prepareStatement("select * from register where r_id=?");
            ps.setInt(1, id);
            ResultSet rset= ps.executeQuery();
            rset.next();
            r.setRId(rset.getInt(1));
            r.setFullName(rset.getString(2));
            r.setUserName(rset.getString(3));
            r.setPassword(rset.getString(4));
            r.setType(rset.getString(5));
            r.setEmail(rset.getString(6));
            r.setPhone(rset.getString(7));
            
            return r;
        } catch (SQLException ex) {
            Logger.getLogger(RegisterDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
        
    public Register getByUsername(String username) {
        try {
            Register r=new Register();
            
            PreparedStatement ps=c.prepareStatement("select * from register where user_name=?");
            ps.setString(1, username);
            ResultSet rset= ps.executeQuery();
            rset.next();
            r.setRId(rset.getInt(1));
            r.setFullName(rset.getString(2));
            r.setUserName(rset.getString(3));
            r.setPassword(rset.getString(4));
            r.setType(rset.getString(5));
            r.setEmail(rset.getString(6));
            r.setPhone(rset.getString(7));
            
            return r;
        } catch (SQLException ex) {
            Logger.getLogger(RegisterDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
        
     public void update(Register r)
    {
        try {
            boolean first=false;
            String updatetxt="";
            if(r.getFullName()!=null)
            {   first=true;
                updatetxt="full_name='"+r.getFullName()+"'";
            }
            if(r.getUserName()!=null)
            {
                if (first) {
                    updatetxt+=", ";
                }
                 first=true;
                updatetxt+="user_name='"+r.getUserName()+"'";
            }
            if(r.getPassword()!=null)
            {
                  if (first) {
                    updatetxt+=", ";
                }
                   first=true;
                updatetxt+="password='"+r.getPassword()+"'";
            }
            if(r.getType()!=null)
            {
                  if (first) {
                    updatetxt+=", ";
                }
                   first=true;
                updatetxt+="type='"+r.getType()+"'";
            }
            if(r.getEmail()!=null)
            {
                  if (first) {
                    updatetxt+=", ";
                }
                   first=true;
                updatetxt+="email='"+r.getEmail()+"'";
            }
            if(r.getPhone()!=null)
            {
                  if (first) {
                    updatetxt+=", ";
                }
                   first=true;
                updatetxt+="phone='"+r.getPhone()+"'";
            }
        
            System.out.println("update register set "+updatetxt+" where r_id=?");
            PreparedStatement ps=c.prepareStatement("update register set "+updatetxt+" where r_id=?");
            ps.setInt(1, r.getRId());
            ps.executeUpdate();
            
        } catch (SQLException ex) {
            Logger.getLogger(RegisterDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    
    }      
    
     public void deleteByName(String username)
    {
        try {
            PreparedStatement ps=c.prepareStatement("delete from register where user_name=?");
            ps.setString(1, username);
            ps.execute();
        } catch (SQLException ex) {
            Logger.getLogger(RegisterDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
       public void deleteById(Integer id)
    {
        try {
            PreparedStatement ps=c.prepareStatement("delete from register where r_id=?");
            ps.setInt(1, id);
            ps.execute();
        } catch (SQLException ex) {
            Logger.getLogger(RegisterDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void main(String [] args)
    {
        try {
            String url ="jdbc:mysql://localhost:3306/dimensionproject";
                  DriverManager.registerDriver(new com.mysql.jdbc.Driver() );
                Connection c=DriverManager.getConnection(url, "root", "root");
              
                new RegisterDao(c).deleteByName("mostafa");
               
                System.out.println("Delete .. .. . ");
        } catch (SQLException ex) {
            Logger.getLogger(RegisterDao.class.getName()).log(Level.SEVERE, null, ex);
        }
           
    
    }
}
